package com.example.myapplication.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.MainActivity;
import com.example.myapplication.database.DatabaseHelper;
import com.example.myapplication.databinding.ItemListBinding;
import com.example.myapplication.model.Note;
import com.example.myapplication.ui.EditActivity;

import java.util.ArrayList;
import java.util.List;

public class NoteListAdapter extends RecyclerView.Adapter<NoteListAdapter.VH> {
    private List<Note> list = new ArrayList<>();

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemListBinding binding = ItemListBinding.inflate(layoutInflater, parent, false);
        return new VH(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        Note note = list.get(position);
        holder.binding.contentTv.setText(note.getTitle() != null ? note.getTitle() : note.getContent());
        holder.binding.timeTv.setText(note.getUpdatedAt() != null ? note.getUpdatedAt() :note.getCreatedAt());

        // 长按删除
        holder.itemView.setOnLongClickListener(view -> {
            showDeleteDialog(note, view.getContext(), position);
            return true;
        });

        // 点击编辑
        holder.itemView.setOnClickListener(view -> {
            MainActivity activity = (MainActivity) view.getContext();
            Intent intent = new Intent(activity, EditActivity.class);
            intent.putExtra("Note", note);
            activity.startActivityForResult(intent, 1001);
        });
    }

    private void showDeleteDialog(Note note, Context context, int position) {
        new AlertDialog.Builder(context)
                .setTitle("温馨提醒")
                .setMessage("确定删除吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", (dialog, which) -> {
                    DatabaseHelper dbHelper = new DatabaseHelper(context);
                    boolean success = dbHelper.deleteNoteById(note.getId());

                    if (success) {
                        list.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, list.size());
                        Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void update(List<Note> noteList) {
        this.list.clear();
        this.list.addAll(noteList);
        notifyDataSetChanged();
    }

    static class VH extends RecyclerView.ViewHolder {
        ItemListBinding binding;

        public VH(@NonNull ItemListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
